﻿#include <assert.h>
#include "simple_regex.h"

int main() {
	{
		simple_regex rgx(L"\\b\\w+nn\\b");
		assert(rgx.valid());
		regex_reader reader(LR"(d--"Backsheeshnn! Backsheesh!"--An Arab)");
		assert(rgx.search(reader));
	}
	{
		simple_regex rgx(L"[\"'][^\"']{0,30}[?!\\.][\"']");
		assert(rgx.valid());
		regex_reader reader(LR"(d--"Backsheesh! Backsheesh!"--An Arab)");
		assert(rgx.search(reader));
	}
	{
		simple_regex rgx(LR"((?:\s*\n)+)");
		assert(rgx.valid());
		regex_reader reader(L".\n\n");
		assert(rgx.search(reader));
		assert(rgx.begin().offset == 1);
		assert(rgx.end().offset == 3);
	}
	{
		simple_regex rgx(LR"((?:\S*?-){2}\w+)");
		assert(rgx.valid());
		assert(rgx.search(regex_reader(LR"( --"Horse-Billiards"--The "Synagogue"--The Writing School--Jack's "Journal")")));
		assert(rgx.begin().offset == 2);
		assert(rgx.end().offset == 19);
	}
	{
		simple_regex rgx(LR"((?:\S*?-){2}\w+)");
		assert(rgx.valid());
		assert(rgx.search(regex_reader(LR"(--"Horse-Billiards"--The "Synagogue"--The Writing School--Jack's "Journal")")));
		assert(rgx.begin().offset == 1);
		assert(rgx.end().offset == 18);
	}
	{
		simple_regex rgx(LR"(.*[/\\](.*\.(cpp|cxx|cc|c\+\+|c|hpp|hxx|hh|h|ixx)|(header|include|inc|(stlport([/\\]using)?)|(c\+\+[/\\]v?[.\d]+.*))[/\\]([-\w]+(\.tcc)?)))");
		assert(rgx.valid());
		assert(rgx.full_match(regex_reader(LR"(t\3200.txt)")) == false);
	}
	{
		simple_regex rgx(L"(?:.*?;)?server");
		assert(rgx.search(regex_reader(L"19947;server_port=20692-20693")));
		assert(rgx.begin().offset == 5);
		assert(rgx.end().offset == 12);
	}
	{
		simple_regex rgx(L"source=(.+?);(?:.*;)*server_port=(\\d+)-(\\d+)(;.*)?");
		assert(rgx.search(regex_reader(L";source=10;p;;o;server_port=3-3//;")));
		auto &cap0 = rgx.get_capture(0);
		auto &cap1 = rgx.get_capture(1);
		auto &cap2 = rgx.get_capture(2);
		auto &cap3 = rgx.get_capture(3);
		assert(cap0.valid());
		assert(cap1.valid());
		assert(cap2.valid());
		assert(!cap3.valid());
		assert(cap0.begin().offset==8);
		assert(cap0.end().offset==10);
	}
	{
		simple_regex rgx(L"(.*?,){2}z");
		rgx.search(regex_reader(L"asdfasd,sksk,ersedf,zsse,ees,z"));
		assert(rgx.begin().offset == 8);
		assert(rgx.end().offset == 21);
		auto &cap = rgx.get_capture(0);
		assert(cap.valid());
		assert(cap.begin().offset == 13);
		assert(cap.end().offset == 20);
	}
	{
		simple_regex rgx(L"[a-zA-Z]+ing");
		rgx.search(regex_reader(L"asdfaling thing"));
		assert(rgx.begin().offset == 0);
		assert(rgx.end().offset == 9);
	}
	{
		simple_regex rgx(L"[a-zA-Z]+ing");
		rgx.search(regex_reader(L"singing"));
		assert(rgx.begin().offset == 0);
		assert(rgx.end().offset == 7);
	}
	{
		simple_regex rgx(L"(?:.+?e){2}");
		rgx.search(regex_reader(L"ThisIsSimple Simple Simle Test"));
		assert(rgx.begin().offset == 10);
		assert(rgx.end().offset == 19);
	}
	{
		simple_regex rgx(L"Simple Simle");
		rgx.search(regex_reader(L"ThisIsSimple Simple Simle Test"));
		assert(rgx.begin().offset == 13);
		assert(rgx.end().offset == 25);
	}
	{
		simple_regex rgx(L".{0,4}(?:Tom|Sawyer|Huckleberry|Finn)");
		assert(rgx.search(regex_reader(L"\"My name's Sawyer.  You don't know me, but that don't")) == true);
		assert(rgx.begin().offset == 7);
		assert(rgx.end().offset == 17);
	}
	{
		simple_regex rgx(L"Tom|Sawyer|Huckleberry|Finn");
		assert(rgx.search(regex_reader(L"\"My name's Sawyer.  You don't know me, but that don't")) == true);
		assert(rgx.begin().offset == 11);
		assert(rgx.end().offset == 17);
	}
	{
		simple_regex rgx(L".*abc");
		assert(rgx.search(regex_reader(L"defabddabce")) == true);
		assert(rgx.begin().offset == 0);
		assert(rgx.end().offset == 10);
	}
	{
		simple_regex rgx(L"[a-zA-Z]+ing");
		assert(rgx.search(regex_reader(L"baaddefing")) == true);
		assert(rgx.begin().offset == 0);
		assert(rgx.end().offset == 10);
	}
	{
		simple_regex rgx(L".{4}abc");
		assert(rgx.search(regex_reader(L"defabddabce")) == true);
		assert(rgx.begin().offset == 3);
		assert(rgx.end().offset == 10);
	}
	{
		simple_regex rgx(L"\\s[a-zA-Z]{0,12}ing\\s");
		assert(rgx.search(regex_reader(L"\n\n")) == false);
	}
	{
		simple_regex rgx(L"[a-z]shing");
		assert(rgx.search(regex_reader(L"wishing to join the Excursion there.")) == true);
	}
	{
		simple_regex rgx(LR"(.*\\(.*(\.(h(pp|xx|h)?))))");
		assert(rgx.full_match(regex_reader(LR"(E:\projects\cpp\edx\advanced_search.h)")) == true);
	}
	{
		simple_regex rgx(LR"(.*(/|\\).*\.(html|xhtml|htm|dhtml))");
		assert(rgx.full_match(regex_reader(LR"(E:\projects\cpp\edx\advanced_search.html)")) == true);
	}
	{
		simple_regex rgx(L"[a-q][^u-z]{13}x");
		assert(rgx.search(regex_reader(L"iz declined,\nexcept")) == false);
	}
	return 0;
}
